﻿using Microsoft.Xna.Framework.Graphics;
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace MyCraft.Minecraft_2D.Blocs
{
    public class Bloc
    {
        #region Attributs
        internal int _id;
        internal int _light, _minLight;
        internal string _name;
        internal int _percent;
        internal int _maxLife, _life;
        internal int _hit;
        internal bool _inventory;
        internal Texture2D _image;
        internal Color _color;
        internal double _startTimer;
        internal int _waitTime;
        public delegate Bloc Actionbloc(Block b, Vector2 p, double t);
        internal Actionbloc _actionBloc;
        #endregion

        #region Constructeur
        public Bloc(double timer, int maxLife, bool inventory = true)
        {
            _id = 0;
            _name = "Air";
            _percent = 0;
            _image = null;
            _startTimer = timer;
            _light = -2;
            _minLight = 5;
            _inventory = inventory;
            _maxLife = maxLife;
            _life = maxLife;
            _hit = 1;
            _color = Color.Transparent;
            _actionBloc = null;
        }
        #endregion

        #region Propriétes
        public int Id
        {
            get { return _id; }
        }

        public string Name
        {
            get { return _name; }
        }

        public int Light
        {
            get { return _light; }
        }

        public int MinLight
        {
            get { return _minLight; }
        }

        public int Percent
        {
            get { return _percent; }
        }

        public int MaxLife
        {
            get { return _maxLife; }
        }

        public int Life
        {
            get { return _life; }
            set
            {
                _life = (int)MathHelper.Clamp(value, 0, _maxLife);
            }
        }

        public int Hit
        {
            get { return _hit; }
        }

        internal Actionbloc ActionBloc
        {
            get { return _actionBloc; }
        }

        public Texture2D Image
        {
            get { return (_image == null) ? BlocSet.GetSingleton()._textures[(TypeBloc)_id] : _image; }
            set { _image = value; }
        }

        public Color Color
        {
            get { return _color; }
        }
        #endregion

        #region Methodes

        public virtual TypeBloc OnNew(Block map)
        {
            return (TypeBloc)this._id;
        }

        public virtual bool IsOK(Block block)
        {
            return true;
        }

        public virtual Bloc OnLife(double timer)
        {
            if (_life <= 0)
                return BlocSet.GetSingleton()._newBlocs[TypeBloc.Air].Invoke(new Vector2(0.5f), timer);
            return this;
        }

        public virtual Bloc Update(Block block, double timer)
        {
            return this;
        }

        public virtual void Refresh(double timer, int min, int max)
        {
            _startTimer = timer;
            _waitTime = new Random((int)timer).Next(min, max);
        }

        public bool OnInventory()
        {
            return _inventory;
        }

        public override string ToString()
        {
            return "{" + _name + ", " + _id + "}";
        }
        #endregion
    }
}
